from flask import Blueprint,jsonify,request,abort
from flask_restful import Api,Resource
from app.models import *
# main = Blueprint("main", __name__)
student = Api()
class Student1(Resource):
    def get(self):
        stu = Student.query.all()
        return jsonify({"stu":list(map(convert_list1,stu))})
    def post(self):
        student_add_one = Student(
            request.json.get('local'),
            request.json.get('age'),
            request.json.get('laiyuan')
        )
        db.session.add(student_add_one)
        db.session.commit()
        return jsonify({'student_add_one': convert_list1(student_add_one)})
student.add_resource(Student1, "/student1/")
class Student2(Resource):
    def get(self,uid):
        student_one = Student.query.filter_by(id=uid).first()
        if student_one is None:
            abort(404)
        return jsonify({'student_one':convert_list1(student_one)})

    def put(self,uid):
        student_update = Student.query.get(uid)
        student_update.local = request.json.get('local')
        student_update.age = request.json.get('age')
        student_update.laiyuan = request.json.get('laiyuan')
        db.session.add(student_update)
        db.session.commit()
        return jsonify({'student_update': convert_list1(student_update)})
student.add_resource(Student2, "/student2/<int:uid>/")